using System;
using EntityFrameWork_Mysql_aspcore.Data;
using EntityFrameWork_Mysql_aspcore.Models;
using Microsoft.AspNetCore.Mvc;

namespace EntityFrameWork_Mysql_aspcore.Controllers {
    public class EFCoreController : Controller {
        public LibraryContext _libraryContext;
        public EFCoreController (LibraryContext libraryContext) {
            this._libraryContext = libraryContext;
        }

        public IActionResult Add (Book book) {
            var message = _libraryContext.Add (book) > 0 ? "success" : "failed";
            return Json (new { message = message });
        }

        public IActionResult Delete (string isbn) {
            var message = _libraryContext.Delete (isbn) > 0 ? "success" : "failed";
            return Json (new { message = message });
        }
        public IActionResult Update (Book book) {
            var message = _libraryContext.Update (book) > 0 ? "success" : "failed";
            return Json (new { Message = message });
        }
        public IActionResult QueryByISBN (string isbn) {
            var book = _libraryContext.QueryByISBN (isbn);
            return Json (new { Book = book });
        }

        public IActionResult QueryByAuthor (string author) {
            var books = _libraryContext.QueryByAuthor (author);
            return Json (new { books = books });
        }
        public IActionResult QueryBookByPaging (int pageSize, int page) {
            var books = _libraryContext.QueryBookByPaging (pageSize, page);
            return Json (new { books = books });
        }
        public IActionResult QueryByFixLanguage (string targetLanguage) {
            var message = _libraryContext.QueryFixLanguage ("chinese") > 0 ? "success" : "failed";
            return Json (new { message = message });
        }
    }
}